package top.malegod.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;

import java.util.Date;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.malegod.mapper.UserMapper;
import top.malegod.model.User;
import top.malegod.service.UserService;
import top.malegod.utils.JacksonUtils;
import top.malegod.utils.Utils;
import top.malegod.utils.RedisUtils;
import top.malegod.utils.ServerResponse;

/**
 * Create by IntelliJ IDEA
 *
 * @author: AbelEthan
 * @date-Time: 2018/5/10 15:54
 * @description: 用户实现service接口
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private RabbitTemplate userTemplate;


    @Autowired
    private RabbitTemplate phoneCodeTemplate;

    /**
     * 登录service层的操作
     */
    @Override
    public ServerResponse<User> login(String username, String password) {
        int count = userMapper.findUserCountByUsername(username);
        if (count != 1) {
            return ServerResponse.createError("用户不存在");
        } else {
            String md5 = Utils.md5(password);
            User user = userMapper.findUserByUsernameAndPassword(username, md5);
            if (user != null) {
                user.setPassword(null);
                return ServerResponse.createSuccess(user);
            }
            return ServerResponse.createError("密码错误");
        }
    }

    /**
     * 注册service层的操作
     */
    @Override
    public ServerResponse<User> register(User user) throws JsonProcessingException {
        int count1 = userMapper.findUserCountByUsername(user.getUsername());
        if (count1 == 1) {
            return ServerResponse.createError("用户已存在");
        } else {
            String md5 = Utils.md5(user.getPassword());
            user.setId(Utils.createUUID());
            user.setPassword(md5);
            user.setCreateTime(new Date());
            userTemplate.convertAndSend(JacksonUtils.objectToJson(user));
            return ServerResponse.createSuccess("注册成功");
        }
    }

    /**
     * 检查用户是否有效service层的操作
     */
    @Override
    public ServerResponse checkValid(String username) {
        int count = userMapper.findUserCountByUsername(username);
        if (count == 1) {
            return ServerResponse.createError("该用户名已存在");
        }
        return ServerResponse.createSuccess("success");
    }

    /**
     * 获取登录用户信息service层的操作
     */
    @Override
    public ServerResponse getUserInfo(User user) {
        User user1 = userMapper.findUserByUserId(user.getId());
        if (user1 == null) {
            return ServerResponse.createError();
        }
        user1.setPassword(null);
        return ServerResponse.createSuccess(user1);
    }

    /**
     * 忘记密码service层的操作
     */
    @Override
    public ServerResponse forgetPwd(String phone){
        phoneCodeTemplate.convertAndSend(phone);
        return ServerResponse.createSuccess("请填写验证码");
    }

    /**
     * 忘记密码的重置密码service层的操作
     */
    @Override
    public ServerResponse forgetResetPassword(String username,String phone, String passwordNew, String code) {
        if (!redisUtils.exists(phone)) {
            return ServerResponse.createError("验证码错误");
        }
        int count = userMapper.updatePasswordByPhone(username, Utils.md5(passwordNew));
        if (count != 1) {
            return ServerResponse.createError("修改密码操作失效");
        }
        redisUtils.remove(phone);
        return ServerResponse.createSuccess("修改密码成功");
    }

    /**
     * 登录状态重置密码service层的操作
     */
    @Override
    public ServerResponse resetPassword(String id, String passwordOld, String passwordNew) {
        String password = userMapper.findUserPasswordById(id);
        if (!password.equals(Utils.md5(passwordOld))){
            return ServerResponse.createError("旧密码输入错误");
        }
        int count = userMapper.updatePasswordByPasswordNew(id, Utils.md5(passwordNew));
        if (count != 1) {
            return ServerResponse.createError("error");
        }
        return ServerResponse.createSuccess("修改密码成功");
    }

    /**
     * 登录状态更新个人信息service层的操作
     */
    @Override
    public ServerResponse updateInformation(User user) {
        int count = userMapper.updateUserByUserId(user);
        if (count != 1) {
            return ServerResponse.createError("用户未登录");
        }
        return ServerResponse.createSuccess("更新个人信息成功");
    }

    /**
     * 获取当前登录用户的详情信息，并强制登录service层的操作
     */
    @Override
    public ServerResponse getInformation(User user) {
        User user1 = userMapper.findUserByUserId(user.getId());
        if (user1 != null) {
            user1.setPassword(null);
            return ServerResponse.createSuccess(user1);
        }
        return ServerResponse.createError("用户未登录,无法获取当前用户信息,status=10,强制登录");
    }
}
